<template>
    <a-card :bordered="false">
    <a-form :form="form">
      <div style="font-size: 16px;font-weight: bold;margin-bottom: 30px;">申请产品识别码</div>
      <div>
          <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="2" :lg="2" :md="4" :sm="24" :xs="24">
              <span class="verMidel">选择产品</span>
            </a-col>
            <a-col :xl="8" :lg="8" :md="16" :sm="24" :xs="24">
              <a-select defaultValue="" @change="changeSpecification" style="width:100%;">
                <a-select-option v-for="(val,index) in specification" :key="val.id" :value="index">{{val.productName}}</a-select-option>
              </a-select>
            </a-col>
          </a-row>
          <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="2" :lg="2" :md="4" :sm="24" :xs="24">
              <span class="verMidel">产品批次</span>
            </a-col>
            <a-col :xl="8" :lg="8" :md="16" :sm="24" :xs="24">
              <a-input class="inputSty" disabled v-model="productList.itemPatch"/>
            </a-col>
          </a-row>
          <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="2" :lg="2" :md="4" :sm="24" :xs="24">
              <span class="verMidel">发行数量</span>
            </a-col>
            <a-col :xl="8" :lg="8" :md="16" :sm="24" :xs="24">
              <a-input class="inputSty" disabled v-model="productListTotal" placeholder="发行数量"/>
            </a-col>
          </a-row>
          <!-- <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="2" :lg="2" :md="4" :sm="24" :xs="24">
              <span class="verMidel">物品码载体</span>
            </a-col>
            <a-col :xl="8" :lg="8" :md="16" :sm="24" :xs="24">
                <a-radio-group name="radioGroup" :defaultValue="2" v-model="productList.itemCode" disabled>
                    <a-radio value="1">二维码</a-radio>
                    <a-radio value="2">RFID</a-radio>
                    <a-radio value="3">NFC</a-radio>
                </a-radio-group>
            </a-col>
          </a-row> -->
          <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="2" :lg="2" :md="4" :sm="24" :xs="24">
              <span class="verMidel">备注</span>
            </a-col>
            <a-col :xl="8" :lg="8" :md="16" :sm="24" :xs="24">
              <textarea v-model="remarkVal" style="height:150px;width:100%;"></textarea>
            </a-col>
          </a-row>
          <!-- <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="2" :lg="2" :md="4" :sm="24" :xs="24">
              <span class="verMidel">上传凭证</span>
            </a-col>
            <a-col :xl="8" :lg="8" :md="16" :sm="24" :xs="24">
                <div class="clearfix">
                    <a-upload
                      :data="{'isup':1,'bizPath':'temp'}"
                      :multiple="true"
                      :action="imgUrl"
                      :headers="headers"
                      accept=".jpg,.png"
                      listType="picture-card"
                      :fileList="fileList"
                      @preview="handlePreview"
                      @change="handleChange"
                    >
                      <div v-if="fileList.length < 6">
                        <a-icon type="plus" />
                        <div class="ant-upload-text">Upload</div>
                      </div>
                    </a-upload>
                    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
                      <img alt="example" style="width: 100%" :src="previewImage" />
                    </a-modal>
                </div>
                <div>
                    只能上传jpg/png格式文件，文件不能超过1MB
                </div>
            </a-col>
          </a-row> -->
          <a-row style="padding:0 15px 28px 15px;">
            <a-col :xl="18" :lg="18" :md="24" :sm="24" :xs="24">
              <a-button type="primary" style="width: 140px;height: 40px;margin-left: 25%;" @click="submit">提交</a-button type="primary">
            </a-col>
          </a-row>        
      </div>
        </a-form>
    </a-card>
  </template>
  
  <script>
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import {initDictOptions, filterMultiDictText} from '@/components/dict/JDictSelectUtil'
    import { getAction,putAction,postAction } from '@/api/manage'
    import JMultiSelectTag from "@/components/dict/JMultiSelectTag"
    import Vue from 'vue'
    import { ACCESS_TOKEN } from "@/store/mutation-types"
    export default {
      name: "BlockProductcodeList",
      // mixins:[JeecgListMixin],
      components: {
        JMultiSelectTag
      },
      data () {
        return {
          form: this.$form.createForm(this),
          qrcodeApplyNumber:"",
          headers:{},
          imgUrl:window._CONFIG['domianURL']+"/sys/common/upload",
          tab1:"",
          tab2:"",
          tab3:"",
          meauList:[],
          subclass:[],
          specification:[],
          plainOptions:[],
          checkedList:[],
          previewVisible: false,
          previewImage: '',
          privateKey:"",
          remarkVal:"",
          fileList: [],
          productListTotal:"",
          productList:{
            itemCode:"1",
            productInfo:""
          },
        }
      },
      computed: {
        
      },
      created(){
        const token = Vue.ls.get(ACCESS_TOKEN);
        this.headers = {"X-Access-Token":token}
      },
      mounted() {
        this.privateKey = this.randomWord(false,64);
        this.initData();
      },
      methods: {
        //提交表单
        submit:function(){
          if(!this.productName){
            this.$message.warning("请选择产品！")
            return
          }
          if(!this.productList.total){
            this.$message.warning("请输入数量！")
            return
          }
          var pay_image = ""
          if(this.fileList.length>0){
            for(let i=0;i<this.fileList.length;i++){
              if(i==0){
                pay_image+=this.fileList[0].response.message;
              }else{
                pay_image+='/'+this.fileList[i].name;
              }
            }
          }           
          var paramsList={
            qrcodeApplyNumber: this.productList.total,
            productName: this.productName,
            productId: this.tab3,
            payImage: pay_image,
            remark: this.remarkVal
          };
           const that = this;
          postAction("/block/blockProductApply/add", paramsList).then((res) => {
            if (res.success) {
              this.form.resetFields();
              this.$message.warning(res.message);
             
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
              
          })
        },
        //初始化剧本大类数据
        initData(){
          var params={
            categoryId: "1216548480059949057",
            column: "createTime",
            order: "asc",
            field: "",
            pageNo: 1,
            pageSize: 10
          };
          getAction("/block/blockProductStandards/list", params).then((res) => {
            if (res.success) {
              this.specification = res.result.records;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
          })
        },
        //剧本规格选择
        changeSpecification(value,option){
          this.tab3 = option.key;          
          var params={
            id: option.key
          };
          getAction("/block/blockProductApply/getProductStandardsData", params).then((res) => {
            if (res.success) {
              this.productList = res.result;
              this.productListTotal = this.productList.total - this.productList.bindNumber;
              if(this.productListTotal == 0){
                this.$message.warning("发行数量已使用完，请选择其他剧本")
              }
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
          })
          this.productName = this.specification[value].productName;
        },
        //标签
        onChange (checkedList) {
          this.indeterminate = !!checkedList.length && (checkedList.length < plainOptions.length)
          this.checkAll = checkedList.length === plainOptions.length
        },
        //随机生成64位
        randomWord(randomFlag, min, max){
          var str = "",
          range = min,
          arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
      
          // 随机产生
          if(randomFlag){
              range = Math.round(Math.random() * (max-min)) + min;
          }
          for(var i=0; i<range; i++){
              var pos = Math.round(Math.random() * (arr.length-1));
              str += arr[pos];
          }
          return str;
        },
        handleCancel () {
            this.previewVisible = false
        },
        handlePreview (file) {
            this.previewImage = file.url || file.thumbUrl
            this.previewVisible = true
        },
        handleChange ({ fileList }) {
            this.fileList = fileList;
        },
         
      }
    }
  </script>
  <style scoped>
    @import '~@assets/less/common.less';
    .verMidel{
      display: inline-block;
      height: 42px;
      line-height: 42px;
    }
    .inputSty{
      padding: 8px;
      height: 42px;
      box-sizing: border-box;
    }
    .ant-upload-select-picture-card i {
        font-size: 32px;
        color: #999;
    }

    .ant-upload-select-picture-card .ant-upload-text {
        margin-top: 8px;
        color: #666;
    }
  </style>